<?php

namespace addons\xcrontab\controller;

use addons\crontab\controller\Autotask;
use addons\xcrontab\library\RunStatus;
use Exception;
use fast\Http;
use think\Log;

class Executor extends Autotask
{
    /**
     * 初始化方法,最前且始终执行
     */
    public function _initialize()
    {
        // 清除错误
        error_reporting(0);
        ignore_user_abort(true);
        session_write_close();
    }

    public function run()
    {
        $requestUrl = $this->request->url();
        $addonInfo = RunStatus::getAddonInfo();
        if ($addonInfo && $addonInfo['state'] && RunStatus::isActive()) {
            if ($this->request->isPost()) {
                $addonConfig = RunStatus::getAddonConfig();
                $timeout = isset($addonConfig['timeout']) ? $addonConfig['timeout'] : 1800;
                if (RunStatus::enableExecuteScript($timeout)) {
                    // 设置永不超时
                    set_time_limit(0);
                    $starttime = time();
                    if (RunStatus::lockStatus()) {
                        while (true) {
                            //每分钟执行一个次轮询
                            RunStatus::everyminuteTimer();
                            //检查插件是否未激活，否则不执行定时轮询
                            $addonInfo = RunStatus::getAddonInfo();
                            //插件激活&&定时任务激活
                            if ($addonInfo && $addonInfo['state']) {
                                $addonInfo = null;
                                if (RunStatus::isActive()) {
                                    RunStatus::updateStatus();
                                    flush();
                                    try {
                                        //执行官方定时任务插件
                                        parent::index();
                                    } catch (Exception $e) {
                                        Log::error('xcrontab: ' . $e->getMessage());
                                        //异常后释放文件锁，删除进程文件
                                        RunStatus::deletepidFile();
                                        if(RunStatus::dblost($e)) {
                                            //重新激活
                                            Http::sendAsyncRequest($requestUrl);
                                        }
                                        return json(['msg' => "ignore for " . $e->getMessage()]);
                                    }
                                } else {
                                    RunStatus::deletepidFile();
                                    return json(['msg' => "xcrontab end"]);
                                }
                            } else {
                                //插件关闭，或  定时任务关闭，释放文件锁，删除进程文件
                                RunStatus::deletepidFile();
                                return json(['msg' => "ignore for addon unactive"]);
                            }
                            if (RunStatus::overmaxdays($starttime)) {
                                RunStatus::deletepidFile();
                                return json(['msg' => 'over max days']);
                            }
                        }
                    } else {
                        return json(['msg' => "ignore for xconrtab locked"]);
                    }
                } else {
                    return json(['msg' => "ignore for xcrontab running"]);
                }
            } else {
                return json(['msg' => "ignore for request method error"]);
            }
        } else {
            RunStatus::deletepidFile();
            return json(['msg' => "ignore for xcrontab unactive or addon unactive"]);
        }
    }
}
